package presenter;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;


public class AdvancedSchedulerDialog extends JFrame implements WindowListener, ActionListener {

    public static final long serialVersionUID=1;
    JPanel schedule;
    JPanel choices;
    JPanel videoChoose;
    JPanel background;
    JCheckBox vid;
    JCheckBox back;
    //JCheckBox bib;
    JPanel boxes;
    Scheduler scheduler;
    Container sched;
    Timer time;
    SongFile current;
    BackgroundChooser backgroundChooser;
    VideoChooser videoChooser;
    ScriptureChooser bibleChooser;
    JList clone;
    JPanel choosers;
    FontChooser fontChooser;
    SongChooser sc;
    JComboBox bibles;

    public AdvancedSchedulerDialog(Scheduler scheduler) {
        super("Advanced Scheduler");
        
        schedule = new JPanel(new BorderLayout());
        this.scheduler = scheduler;
        clone = new JList(scheduler.song);
        sched = clone;
        
        sched.requestFocusInWindow();
        schedule.add(sched, "West");
        boxes = new JPanel();
        vid = new JCheckBox("Video");
        back = new JCheckBox("Background");
       
        boxes.add(back);
        boxes.add(vid);
        
        back.addActionListener(this);
        vid.addActionListener(this);

        schedule.add(boxes, "North");
        choosers = new JPanel(new GridLayout(4, 1));
        fontChooser = new FontChooser();
        choosers.add(fontChooser);
        schedule.add(choosers, "Center");
        backgroundChooser = new BackgroundChooser();
        videoChooser = new VideoChooser();
        bibleChooser = new ScriptureChooser(BibleInformation.getDefaultBible());
        setContentPane(schedule);
        setSize(800, 600);
        time = new Timer(100, this);
        time.start();
        current = new SongFile();
        addWindowListener(this);
        setVisible(true);
    }

    public void windowOpened(WindowEvent windowevent) {
    }

    public void windowClosing(WindowEvent windowevent) {
    }

    public void windowClosed(WindowEvent e) {
        dispose();
    }

    public void windowIconified(WindowEvent windowevent) {
    }

    public void windowDeiconified(WindowEvent windowevent) {
    }

    public void windowActivated(WindowEvent windowevent) {
    }

    public void windowDeactivated(WindowEvent windowevent) {
    }

    public void actionPerformed(ActionEvent e) {
        SongFile sf = (SongFile)clone.getSelectedValue();
        if(sf == null)
            return;
        if(current != null && sf.getNumber() != current.getNumber()) {
            choosers.remove(fontChooser);
            fontChooser = new FontChooser(sf);
            choosers.add(fontChooser);
            if(current.hasCustomBackground()) {
                choosers.remove(backgroundChooser);
                backgroundChooser = new BackgroundChooser();
            }
            if(current.hasVideo()) {
                choosers.remove(videoChooser);
                videoChooser = new VideoChooser();
            }
            if(current.isScripture()) {
                choosers.remove(bibleChooser);
                bibleChooser=new ScriptureChooser(BibleInformation.getDefaultBible());
            }
            if(current.hasCustomBackground() && current.getBackground() == null)
                current.setHasBackground(false);
            if(current.hasVideo() && current.getVideo() == null)
                current.setHasVideo(false);
            setContentPane(schedule);
            if(sf.hasCustomBackground()) {
                back.setSelected(true);
                backgroundChooser.setSong(sf);
                backgroundChooser.setBackground(sf.getBackground());
                choosers.add(backgroundChooser);
            }
            if(sf.hasVideo()) {
                vid.setSelected(true);
                videoChooser.setSong(sf);
                choosers.add(videoChooser);
            }
            if(sf.isScripture()) {
                System.out.println("bible verse loading");
                bibleChooser.setSong(sf);
                choosers.add(bibleChooser);
                setContentPane(schedule);
            }

        }
        current = sf;
        if(e.getSource() == back) {
            if(!sf.hasCustomBackground()) {
                backgroundChooser = new BackgroundChooser();
                choosers.add(backgroundChooser);
                backgroundChooser.setSong(sf);
                sf.setHasBackground(true);
            } else {
                sf.setHasBackground(false);
                choosers.remove(backgroundChooser);
            }
        } else if(e.getSource() == vid) {
            if(!sf.hasVideo()) {
                videoChooser = new VideoChooser();
                videoChooser.setSong(sf);
                sf.setHasVideo(true);
                choosers.add(videoChooser, "Center");
            } else {
                sf.setHasVideo(false);
                choosers.remove(videoChooser);
            }
        } else {
            vid.setSelected(sf.hasVideo());
            back.setSelected(sf.hasCustomBackground());
        }
        if(e.getSource() != time || backgroundChooser.isUpdated() || videoChooser.isUpdated() ||bibleChooser.isUpdated())
            setContentPane(schedule);
    }
    
    private void getBibles() {
        File f=new File("Bibles");
        String[] bibs=f.list(new BibleFilter());
        for(int i=0; i<bibs.length; i++) {
            bibs[i]=bibs[i].substring(0,bibs[i].lastIndexOf(".xmm"));
        }

    }
}
